<?php
/**
 * bs多规格sku商城系统
 * ============================================================================
 * 版权所有 2024-2099 wltx666.com，并保留所有权利。
 * 网站地址: https://wltx666.com
 * ============================================================================
 * Author: 草上飞
 * Email: wltx@alipay168.cn
 * QQ: 2719875001
 * Date: 2025/1/1
 * 描述:
 */

namespace app\result;

use app\model\Navs;

class NavsResult extends BaseResult
{
    public function __construct()
    {
        parent::__construct();
        $this->model = new Navs();
    }

    public function api_list($args = []): array
    {
        $where = [];
        $where[] = ['startTime', '<', time()];
        $where[] = ['endTime', '>', time()];
        $where[] = ['status', '=', 1];
        $data = $this->model->data_list_nopage($where, 'id,title,imgUrl,linkUrl,linkType', 'sort desc,id desc');
        return [
            'data_list' => $data
        ];
    }

    public function list($args = [])
    {
        $page = intval($args['page'] ?? 1);
        $pageSize = intval($args['limit'] ?? 10);
        $status = intval($args['status'] ?? 0);
        $where = [];
        if (!empty($args['keyword'])) {
            $where[] = ['title', 'like', '%' . trim($args['keyword']) . '%'];
        }
        if ($status) {
            $where[] = ['status', '=', $status];
        }
        $total = $this->model->data_count($where);
        $data = $this->model->data_list($where, '*', 'id desc', $page, $pageSize);
        if ($data) {
            foreach ($data as &$item) {
                $item['startTime'] = date('Y-m-d H:i:s', $item['startTime']);
                $item['endTime'] = date('Y-m-d H:i:s', $item['endTime']);
            }
            unset($item);
        }
        return $this->success_return('success', [
            'total' => $total,
            'list' => $data
        ]);
    }

    public function get($args = [])
    {
        $id = intval($args['id'] ?? 0);
        $data = $this->model->data_get(['id' => $id]);
        if (!$data) {
            return $this->error_return('数据不存在');
        }
        $data['startTime'] = date('Y-m-d H:i:s', $data['startTime']);
        $data['endTime'] = date('Y-m-d H:i:s', $data['endTime']);
        return $this->success_return('success', [
            'info' => $data
        ]);
    }

    public function save_data($args): array
    {
        $id = intval($args['id'] ?? 0);

        $baseData = [
            'title' => $args['title'],
            'imgUrl' => $args['imgUrl'],
            'linkUrl' => $args['linkUrl'],
            'linkType' => intval($args['linkType'] ?? 0),
            'startTime' => strtotime($args['startTime']),
            'endTime' => strtotime($args['endTime']),
            'status' => intval($args['status'])
        ];
        if ($id) {
            $this->model->data_update(['id' => $id], $baseData);
        } else {
            $this->model->data_add($baseData);
        }
        return $this->success_return('保存成功');
    }

    public function set_status($args)
    {
        $id = intval($args['id'] ?? 0);
        $status = $this->model->get_value(['id' => $id], 'status');
        $res = (new Navs())->data_update(['id' => $id], ['status' => $status == 1 ? -1 : 1]);
        return $this->success_return('保存成功');
    }

    public function del_data($args): array
    {
        $id = intval($args['id'] ?? 0);
        $res = $this->model->data_del(['id' => $id]);
        return $this->success_return('已删除');
    }
}
